On the right you can see my first attempt to set the PIC microcontroller up to read values from the ADC and display them. I thought that for the first program we would just take the values out of the converter and place them on the LEDs. When the light level changes our LEDs should flicker to represent the new number. On the right you can see a program which does this. It sets up the hardware and then repeatedly reads the light levels and displays them.

The ANSELA register controls which pins are analogue and which are digital. The value of 0x01 sets just ADC0 (i.e. pin A0) to be an analogue input - the rest are digital.

ADCON0 controls which analogue channel we are going to sample. We are using channel 0. This register is also used to initiate ADC conversion and can be read to find out if the ADC conversion has completed.

ADCON1 contains some further options relating to the ADC module. The only one we are concerned with is setting the justification of the results. This is important because the ADC result is 10-bits, which are split over 2 8-bit registers (ADRESL and ADRESH). The value of 0x80 sets it to be right-justified - i.e. the ADC result fills the right side of those two registers and the top 6 bits of the ADRESH (the high byte) are always zero.